import api from '@/api';

export default {
  namespace: 'base',

  state: {
    memberLevels: [],
    signRuleStatus: [],
    paymentStatus: [],
    productTags: [],
    productBrands: [],
    warehouseTypes: [],
    cargoTypes: [],
    timeUnits: [],
    authStatus: [],
    ratings: [],
    serviceTypes: [],
    weightSchemas: {
      list: [],
      pagination: {},
    },
    regionSchemas: {
      list: [],
      pagination: {},
    },
    currentRegionSchema: {},
    regions: {
      list: [],
      pagination: {},
    },
    scopeRegions: [],
    airports: {
      list: [],
      pagination: {},
    },
    airportType: [],
    airlines: {
      list: [],
      pagination: {},
    },
    airlineType: [],
    airlineCompanies: {
      list: [],
      pagination: {},
    },
    airFlights: {
      list: [],
      pagination: {},
    },
    flights: {
      list: [],
      pagination: {}
    },
    flightTypes: [],
    flightStatus: [],
    numberRules: {
      list: [],
      pagination: {},
    },
    numberRuleType: [],
    numberVerifyRule: [],
    numberBatchs: {
      list: [],
      pagination: {},
    },
    auditNumbers: {
      list: [],
      pagination: {},
    },
    costTypes: {
      list: [],
      pagination: {},
    },
    cargoFathers: {
      list: [],
      pagination: {},
    },
    identityStatus: {},
    resourceType: {},
    serviceType: []
  },

  effects: {
    *queryCountries({ payload }, { call }) {
      return yield call(api.base.queryCountries, payload)
    },
    *queryLatestExchangeList({ payload }, { call }) {
      return yield call(api.base.queryLatestExchangeList, payload);
    },
    *createExchange({ payload }, { call }) {
      return yield call(api.base.createExchange, payload)
    },
    *queryExchangeList({ payload }, { call }) {
      return yield call(api.base.queryExchangeList, payload)
    },
    *queryMoney(_, { call }) {
      return yield call(api.base.queryMoney)
    },
    *queryWeightRule(_, { call }) {
      return yield call(api.weightSchema.queryWeightRule);
    },
    *queryWeightDetails({ payload }, { call }) {
      return yield call(api.weightSchema.queryWeightDetails, payload);
    },
    *fetchAirFlights({ payload }, { call, put }) {
      const res = yield call(api.base.queryAirFlight, payload);
      if (res.errorCode === 0) {

        yield put({
          type: 'saveAirFlight',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *createAirFlight({ payload }, { call }) {
      const res = yield call(api.base.createAirFlight, payload);
      return res;
    },
    *updateAirFlight({ payload }, { call }) {
      const res = yield call(api.base.updateAirFlight, payload);
      return res;
    },
    *removeAirFlight({ payload }, { call }) {
      const res = yield call(api.base.removeAirFlight, payload);
      return res;
    },
    *disableAirFlight({ payload }, { call }) {
      const res = yield call(api.base.disableAirFlight, payload);
      return res;
    },
    *openAirFlight({ payload }, { call }) {
      const res = yield call(api.base.openAirFlight, payload);
      return res;
    },
    *fetchAirFlightById({ payload }, { call }) {
      const res = yield call(api.base.queryAirFlightById, payload);
      return res;
    },
    *fetchAirports({ payload }, { call, put }) {
      const res = yield call(api.base.queryAirport, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveAirports',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *removeAirports({ payload }, { call }) {
      const res = yield call(api.base.removeAirport, payload);
      return res;
    },
    *createAirport({ payload }, { call }) {
      const res = yield call(api.base.createAirport, payload);
      return res;
    },
    *updateAirport({ payload }, { call }) {
      const res = yield call(api.base.updateAirport, payload);
      return res;
    },
    *fetchAirportType(_, { call, put }) {
      const res = yield call(api.base.queryAirportType);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveAirportType',
          payload: res.data,
        });
      }
    },
    *fetchAirlines({ payload }, { call, put }) {
      const res = yield call(api.base.queryAirlines, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveAirlines',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *removeAirlines({ payload }, { call }) {
      const res = yield call(api.base.removeAirlines, payload);
      return res;
    },
    *createAirline({ payload }, { call }) {
      const res = yield call(api.base.createAirline, payload);
      return res;
    },
    *updateAirline({ payload }, { call }) {
      const res = yield call(api.base.updateAirline, payload);
      return res;
    },
    *fetchAirlineType(_, { call, put }) {
      const res = yield call(api.base.queryAirlineType);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveAirlineType',
          payload: res.data,
        });
      }
    },
    *fetchGroup({ payload }, { call }) {
      return yield call(api.base.queryGroup, payload);
    },
    *fetchAreasByCode({ payload }, { call }) {
      return yield call(api.base.queryAreasByCode, payload);
    },
    *fetchAreas({ payload }, { call, put }) {
      return yield call(api.base.queryAreas, payload);
    },
    *fetchWarehouseTypes({ payload }, { call, put }) {
      let res = yield call(api.base.queryWarehouseTypes);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveWarehouseTypes',
          payload: res.data,
        });
      }
      return res;
    },
    *fetchCargoTypes(_, { call, put }) {
      let res = yield call(api.base.queryCargoTypes);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCargoTypes',
          payload: res.data,
        });
      }
    },
    *fetchTimeUnits(_, { call, put }) {
      let res = yield call(api.base.queryTimeUnits);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveTimeUnits',
          payload: res.data,
        });
      }
    },
    *fetchAuthStatus(_, { call, put }) {
      let res = yield call(api.base.queryAuthStatus);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveAuthStatus',
          payload: res.data,
        });
      }
    },
    *fetchRating(_, { call, put }) {
      let res = yield call(api.base.queryRating);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveRating',
          payload: res.data,
        });
      }
    },
    *fetchServiceTypes(_, { call, put }) {
      const response = yield call(api.base.queryServiceTypes);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveServiceTypes',
          payload: response.data,
        });
      }
    },
    *fetchGroupInfo(_, { call }) {
      return yield call(api.base.queryGroupInfo);
    },
    *fetchWeightSchemas({ payload }, { call, put }) {
      let res = yield call(api.weightSchema.query, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveWeightSchemas',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
      return res;
    },
    *deleteWeightSchema({ payload }, { call }) {
      return yield call(api.weightSchema.remove, payload);
    },
    *addWeightSchema({ payload }, { call }) {
      return yield call(api.weightSchema.create, payload);
    },
    *updateWeightSchema({ payload }, { call }) {
      return yield call(api.weightSchema.update, payload);
    },
    *fetchRegionSchemas({ payload }, { call, put }) {
      let res = yield call(api.region.schema.query, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveRegionSchemas',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
      return res;
    },
    *deleteRegionSchema({ payload }, { call }) {
      return yield call(api.region.schema.remove, payload);
    },
    *addRegionSchema({ payload }, { call }) {
      return yield call(api.region.schema.create, payload);
    },
    *updateRegionSchema({ payload }, { call }) {
      return yield call(api.region.schema.update, payload);
    },
    *fetchRegions({ payload }, { call, put }) {
      let res = yield call(api.region.query, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveRegions',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
      return res;
    },
    *deleteRegion({ payload }, { call }) {
      return yield call(api.region.remove, payload);
    },
    *addRegion({ payload }, { call }) {
      return yield call(api.region.create, payload);
    },
    *updateRegion({ payload }, { call }) {
      return yield call(api.region.update, payload);
    },
    *putRegion({ payload }, { call }) {
      return yield call(api.region.updateBySchemaId, payload);
    },
    *getRegions({ payload }, { call, put }) {
      let res = yield call(api.region.queryBySchemaId, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveScopeRegions',
          payload: res.data,
        });
      }
      return res;
    },
    *fetchAirlineCompanies({ payload }, { call, put }) {
      const res = yield call(api.base.queryCarrierCompanies, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveAirlineCompanies',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *fetchCarrierCompanyInfo({ payload }, { call, put }) {
      const res = yield call(api.base.queryCarrierCompanyInfo, payload);
      return res;
    },
    *removeCarrierCompanies({ payload }, { call }) {
      const res = yield call(api.base.removeCarrierCompanies, payload);
      return res;
    },
    *createCarrierCompany({ payload }, { call }) {
      const res = yield call(api.base.createCarrierCompany, payload);
      return res;
    },
    *updateCarrierCompany({ payload }, { call }) {
      const res = yield call(api.base.updateCarrierCompany, payload);
      return res;
    },
    *checkCarrierCompanyCode({ payload }, { call }) {
      const res = yield call(api.base.checkCarrierCompanyCode, payload);
      return res;
    },
    *fetchNumberRules({ payload }, { call, put }) {
      const res = yield call(api.base.queryNumberRules, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveNumberRules',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *fetchNumberRulesShort({ payload }, { call, put }) {
      const res = yield call(api.base.queryNumberRulesShort, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveNumberRules',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *fetchNumberRuleInfo({ payload }, { call, put }) {
      const res = yield call(api.base.queryNumberRuleByID, payload);
      return res;
    },
    *enableNumberRule({ payload }, { call, put }) {
      const res = yield call(api.base.enableNumberRule, payload);
      return res;
    },
    *disableNumberRule({ payload }, { call, put }) {
      const res = yield call(api.base.disableNumberRule, payload);
      return res;
    },
    *fetchNumberRuleType({ payload }, { call, put }) {
      const res = yield call(api.base.queryNumberRuleType, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveNumberRuleType',
          payload: res.data,
        });
      }
    },
    *fetchNumberVerifyRule({ payload }, { call, put }) {
      const res = yield call(api.base.queryNumberVerifyRule, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveNumberVerifyRule',
          payload: res.data,
        });
      }
    },
    *createNumberRule({ payload }, { call }) {
      const res = yield call(api.base.createNumberRule, payload);
      return res;
    },
    *updateNumberRule({ payload }, { call }) {
      const res = yield call(api.base.updateNumberRule, payload);
      return res;
    },
    *generateNumber({ payload }, { call }) {
      const res = yield call(api.base.generateNumber, payload);
      return res;
    },
    *generateLotsNumber({ payload }, { call }) {
      const res = yield call(api.base.generateLotsNumber, payload);
      return res;
    },
    *checkNumberRuleName({ payload }, { call, put }) {
      return yield call(api.base.checkNumberRuleName, payload);
    },

    // 号码库存
    *fetchNumberBatchs({ payload }, { call, put }) {
      const res = yield call(api.base.queryNumberBatchs, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveNumberBatchs',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *createNumberBatch({ payload }, { call }) {
      const res = yield call(api.base.createNumberBatch, payload);
      return res;
    },
    *numberBatchCheckNumber({ payload }, { call }) {
      const res = yield call(api.base.numberBatchCheckNumber, payload);
      return res;
    },
    *importNumber({ payload }, { call }) {
      const res = yield call(api.base.importNumber, payload);
      return res;
    },
    *fetchNumberBatchInfo({ payload }, { call, put }) {
      const res = yield call(api.base.queryNumberBatchByID, payload);
      return res;
    },
    *fetchStartNumber({ payload }, { call, put }) {
      const res = yield call(api.base.queryStartNumber, payload);
      return res;
    },
    *configGenRule({ payload }, { call }) {
      const res = yield call(api.base.configGenRule, payload);
      return res;
    },
    // 号码监管
    *fetchAuditNumbers({ payload }, { call, put }) {
      const res = yield call(api.base.queryfAuditNumbers, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'savefAuditNumbers',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },

    *fetchProductTags(_, { call, put }) {
      const res = yield call(api.base.queryProductTags);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveProductTags',
          payload: res.data,
        });
      }
    },
    *fetchProductBrands(_, { call, put }) {
      const res = yield call(api.base.queryProductBrands);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveProductBrands',
          payload: res.data,
        });
      }
    },
    *fetchCostTypeFather(_, { call, put }) {
      const res = yield call(api.base.costTypeFather);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCostTypeFather',
          payload: res.data,
        });
      }
    },
    *fetchCostTypeChild({ payload }, { call }) {
      const res = yield call(api.base.costTypeChild, payload);
      return res;
    },
    *saveCostType({ payload }, { call }) {
      const res = yield call(api.base.saveCostType, payload);
      return res;
    },
    *updateCostType({ payload }, { call }) {
      const res = yield call(api.base.updateCostType, payload);
      return res;
    },
    *deleteCostType({ payload }, { call }) {
      const res = yield call(api.base.deleteCostType, payload);
      return res;
    },
    *fetchCargoTypeFather({ payload }, { call, put }) {
      const res = yield call(api.base.cargoFatherType, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCargoTypeFather',
          payload: res.data,
        });
      }
    },
    *queryIdentityStatus({ payload }, { call, put }) {
      const res = yield call(api.base.queryIdentityStatus, payload)
      if (res.errorCode === 0) {
        yield put({
          type: 'saveIdentityStatus',
          payload: res.data,
        });
      }
    },
    *queryResourceType({ payload }, { call, put }) {
      const res = yield call(api.base.queryResourceType, payload)
      if (res.errorCode === 0) {
        yield put({
          type: 'saveResourceType',
          payload: res.data,
        });
      }
    },
    *fetchCargoTypeChild({ payload }, { call }) {
      const res = yield call(api.base.cargoChildType, payload);
      return res;
    },
    *saveCargoType({ payload }, { call }) {
      const res = yield call(api.base.saveCargoType, payload);
      return res;
    },
    *deleteCargoType({ payload }, { call }) {
      const res = yield call(api.base.deleteCargoType, payload);
      return res;
    },
    *updateCargoType({ payload }, { call }) {
      const res = yield call(api.base.updateCargoType, payload);
      return res;
    },
    *fetchServiceType({ payload }, { call, put }) {
      let res = yield call(api.base.serviceType, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveServiceType',
          payload: res.data
        });
      }
    },
    *fetchFlights({ payload }, { call, put }) {
      const res = yield call(api.base.queryFlight, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveFlight',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *fetchDateFlights({ payload }, { call, put }) {
      const res = yield call(api.base.queryDateFlight, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveFlight',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *fetchFlightStatus({ payload }, { call, put }) {
      let res = yield call(api.base.queryFlightStatus, payload);
      return res
    },
    *fetchFlightTypes({ payload }, { call, put }) {
      let res = yield call(api.base.queryDateFlightType, payload);
      return res
    },
    *fetchPaymentStatus({ payload }, { call, put }) {
      const res = yield call(api.base.paymentStatus, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'savePaymentStatus',
          payload: res.data,
        });
      }
    },
    *fetchSignRuleStatus({ payload }, { call, put }) {
      const res = yield call(api.base.signRuleStatus, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveSignRuleStatus',
          payload: res.data,
        });
      }
    },
    *fetchMemberLevel({ payload }, { call, put }) {
      const response = yield call(api.base.queryMemberLevel, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveMemberLevel',
          payload: response.data,
        });
      }
      return response;
    },
  },
  reducers: {
    saveMemberLevel(state, { payload }) {
      return {
        ...state,
        memberLevels: payload,
      };
    },
    saveSignRuleStatus(state, { payload }) {
      return {
        ...state,
        signRuleStatus: payload
      };
    },
    savePaymentStatus(state, { payload }) {
      return {
        ...state,
        paymentStatus: payload
      };
    },
    saveResourceType(state, { payload }) {
      return {
        ...state,
        resourceType: payload
      };
    },
    saveIdentityStatus(state, { payload }) {
      return {
        ...state,
        identityStatus: payload
      };
    },
    saveCargoTypeFather(state, { payload }) {
      return {
        ...state,
        cargoFathers: {
          list: payload,
          pagination: {}
        }
      };
    },
    saveCostTypeFather(state, { payload }) {
      return {
        ...state,
        costTypes: {
          list: payload,
          pagination: {}
        }
      };
    },
    saveAirFlight(state, { payload }) {
      return {
        ...state,
        airFlights: payload,
      };
    },
    saveAirports(state, { payload }) {
      return {
        ...state,
        airports: payload,
      };
    },
    saveAirportType(state, { payload }) {
      return {
        ...state,
        airportType: payload,
      };
    },
    saveAirlines(state, { payload }) {
      return {
        ...state,
        airlines: payload,
      };
    },
    saveAirlineType(state, { payload }) {
      return {
        ...state,
        airlineType: payload,
      };
    },
    saveWarehouseTypes(state, { payload }) {
      return {
        ...state,
        warehouseTypes: payload,
      };
    },
    saveCargoTypes(state, { payload }) {
      return {
        ...state,
        cargoTypes: payload,
      };
    },
    saveTimeUnits(state, { payload }) {
      return {
        ...state,
        timeUnits: payload,
      };
    },
    saveAuthStatus(state, { payload }) {
      return {
        ...state,
        authStatus: payload,
      };
    },
    saveRating(state, { payload }) {
      return {
        ...state,
        ratings: payload,
      };
    },
    saveServiceTypes(state, { payload }) {
      return {
        ...state,
        serviceTypes: payload,
      };
    },
    saveWeightSchemas(state, { payload }) {
      return {
        ...state,
        weightSchemas: payload,
      };
    },
    saveRegionSchemas(state, { payload }) {
      return {
        ...state,
        regionSchemas: payload,
      };
    },
    saveRegions(state, { payload }) {
      return {
        ...state,
        regions: payload,
      };
    },
    saveScopeRegions(state, { payload }) {
      return {
        ...state,
        scopeRegions: payload,
      };
    },
    saveAirlineCompanies(state, { payload }) {
      return {
        ...state,
        airlineCompanies: payload,
      };
    },
    saveNumberRules(state, { payload }) {
      return {
        ...state,
        numberRules: payload,
      };
    },
    saveNumberRuleType(state, { payload }) {
      return {
        ...state,
        numberRuleType: payload,
      };
    },
    saveNumberVerifyRule(state, { payload }) {
      return {
        ...state,
        numberVerifyRule: payload,
      };
    },
    saveNumberBatchs(state, { payload }) {
      return {
        ...state,
        numberBatchs: payload,
      };
    },
    savefAuditNumbers(state, { payload }) {
      return {
        ...state,
        auditNumbers: payload,
      };
    },
    saveProductTags(state, { payload }) {
      return {
        ...state,
        productTags: payload,
      };
    },
    saveProductBrands(state, { payload }) {
      return {
        ...state,
        productBrands: payload,
      };
    },
    saveServiceType(state, { payload }) {
      return {
        ...state,
        serviceType: payload,
      };
    },
    saveFlight(state, { payload }) {
      return {
        ...state,
        flights: payload,
      };
    }
  },
};
